<?php
/**
 * 发货通知单推送通知仓库封装类
 *
 * @author xiayuanjun
 * @Time: 2016-04-20
 * @version 0.1
 */

class ome_delivery_notice{

    /**
     * @description 修改配送信息
     * @access public
     * @param int $delivery_id 发货单ID
     * @return void
     */
    static public function create($delivery_id) {

        $operationLogObj = app::get('ome')->model('operation_log');
        $operationLogObj->write_log('delivery_modify@ome',$delivery_id,"发货通知单推送仓库开始",NULL,$opInfo);

        $dlySendObj = app::get('console')->model('delivery_send');
        $dlySendObj->update_send_status($delivery_id,'sending');

        $original_data = kernel::single('ome_event_data_delivery')->generate($delivery_id);

        //根据仓库识别是否门店仓还是电商仓
        $store_id = kernel::single('ome_branch')->isStoreBranch($original_data['branch_id']);
        if($store_id){
            $channel_type = 'store';
            $channel_id = $store_id;
        }else{
            $wms_id = kernel::single('ome_branch')->getWmsIdById($original_data['branch_id']);
            self::_get_electron_logi_no($wms_id,$original_data);

            $channel_type = 'wms';
            $channel_id = $wms_id;
        }

        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        $result = $dlyTriggerLib->create($channel_type,$channel_id,$original_data,false);
        if($result['rsp'] == 'fail'){
            $msg = "失败. 原因:".$result['msg'];
        }else{
            $msg ="成功.";

            if (isset($result['data']['wms_order_code'])) {
                $dlyExtObj = app::get('console')->model('delivery_extension');
                $ext_data['delivery_bn'] = $original_data['outer_delivery_bn'];
                $ext_data['original_delivery_bn'] = $result['data']['wms_order_code'];
                $dlyExtObj->create($ext_data);
            }
        }

        if(isset($result['msg_id'])){
            $msg .=" msg_id:".$result['msg_id'];
        }

        $operationLogObj->write_log('delivery_modify@ome',$delivery_id,"推送结果:".$msg,NULL,$opInfo);
    }

    static public function cancel($params, $sync = false){

        $dly_id = $params['delivery_id'];
        $data['outer_delivery_bn'] = $params['delivery_bn'];

        $dlyObj = app::get('ome')->model("delivery");
        $branchObj = app::get('ome')->model("branch");

        //根据仓库识别是否门店仓还是电商仓
        $store_id = kernel::single('ome_branch')->isStoreBranch($params['branch_id']);
        if($store_id){
            $channel_type = 'store';
            $channel_id = $store_id;
        }else{
            $branch =$branchObj->getList('branch_bn',array('branch_id'=>$params['branch_id']),0,1);
            $data['branch_bn'] =$branch[0]['branch_bn'];

            if(isset($params['wms_id'])){
                $wms_id = $params['wms_id'];
            }else{
                $branchLib = kernel::single('ome_branch');
                $wms_id = $branchLib->getWmsIdById($params['branch_id']);
            }

            $channel_type = 'wms';
            $channel_id = $wms_id;
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->cancel($channel_type,$channel_id,$data,$sync);
    }

    static public function pause($params, $sync = false){

        $dly_id = $params['delivery_id'];
        $data['outer_delivery_bn'] = $params['delivery_bn'];

        //根据仓库识别是否门店仓还是电商仓
        $store_id = kernel::single('ome_branch')->isStoreBranch($params['branch_id']);
        if($store_id){
            $channel_type = 'store';
            $channel_id = $store_id;
        }else{
            if(isset($params['wms_id'])){
                $wms_id = $params['wms_id'];
            }else{
                $branchLib = kernel::single('ome_branch');
                $wms_id = $branchLib->getWmsIdById($params['branch_id']);
            }

            $channel_type = 'wms';
            $channel_id = $wms_id;
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->pause($channel_type,$channel_id,$data,$sync);
    }

    static public function renew($params, $sync = false){

        $dly_id = $params['delivery_id'];
        $data['outer_delivery_bn'] = $params['delivery_bn'];

        //根据仓库识别是否门店仓还是电商仓
        $store_id = kernel::single('ome_branch')->isStoreBranch($params['branch_id']);
        if($store_id){
            $channel_type = 'store';
            $channel_id = $store_id;
        }else{
            if(isset($params['wms_id'])){
                $wms_id = $params['wms_id'];
            }else{
                $branchLib = kernel::single('ome_branch');
                $wms_id = $branchLib->getWmsIdById($params['branch_id']);
            }

            $channel_type = 'wms';
            $channel_id = $wms_id;
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->renew($channel_type,$channel_id,$data,$sync);
    }

    static public function search($params, $sync = false){

        $dly_id = $params['delivery_id'];

        $dlyExtObj = app::get('console')->model('delivery_extension');
        $dlyExtInfo = $dlyExtObj->dump(array('delivery_bn'=>$params['delivery_bn']),'original_delivery_bn');
        $data = array(
            'delivery_bn'=>$params['delivery_bn'],
            'out_order_code'=>$dlyExtInfo['original_delivery_bn'],    
        );

        if(isset($params['wms_id'])){
            $wms_id = $params['wms_id'];
        }else{
            $branchLib = kernel::single('ome_branch');
            $wms_id = $branchLib->getWmsIdById($params['branch_id']);
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->search($wms_id, $data, $sync);
    }

    static public function _get_electron_logi_no($wms_id,&$original_data){
        //获取目前支持获取WMS 目前只支持科捷和伊藤忠 境宴和360
        $node_type = array('ilc','kejie','cnss','sku360');
        $channel_adapter = app::get('channel')->model('channel');
        $wms_detail = $channel_adapter->dump(array('channel_id'=>$wms_id,'node_id|noequal'=>''),'node_type');
        if (!$original_data['logi_no'] && $original_data['shop_type'] == '360buy' && in_array($wms_detail['node_type'],$node_type))  {

            $dlyCorpObj = app::get('ome')->model('dly_corp');
            $dlyCorp = $dlyCorpObj->dump($original_data['logi_id'], 'channel_id');

            $channel = app::get('logisticsmanager')->model('channel')->dump(array('channel_id'=>$dlyCorp['channel_id'],'status' => 'true'));
            $jdAccount = explode('|||', $channel['shop_id']);

            if ($jdAccount[0]) {
                $params = array(
                    'preNum'       => 1, //运单量数据量
                    'primary_bn'   => $original_data['delivery_bn'],
                    'delivery'     => $original_data,
                );
                $data = kernel::single('erpapi_router_request')->set('logistics', $channel['channel_id'])->electron_directRequest($params);
                $data = current($data);
                if($data['succ']) {
                    $logi_no = trim($data['logi_no']);
                    $original_data['logi_no'] = $logi_no;
                    $tmp_data = array(
                        'delivery_bn' => $original_data['outer_delivery_bn'],
                        'logi_no'     => $logi_no,
                        'action'      => 'addLogiNo',
                        'status'      => 'update',
                    );

                    kernel::single('ome_event_receive_delivery')->update($tmp_data);
                }else{
                    $original_data['logi_no'] = false;
                }
            }
        }
    }


}